/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.*;

import soccerscope.db.GameItem;
import soccerscope.db.DatabaseReader;
import javax.swing.table.TableModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableModel;
import soccerscope.util.analyze.DBAnalyzer;
import java.awt.event.MouseEvent;
import java.awt.event.MouseAdapter;

public class StatisticFrame extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    JSplitPane jSplitPane1 = new JSplitPane();
    JScrollPane jScrollPane1 = new JScrollPane();
    JScrollPane jScrollPane2 = new JScrollPane();
    JList jGameList = new JList();
    JTable jStatisticTable = new JTable();
    public StatisticFrame() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        loadGameName();
        jGameList.setModel(gameItems);
        TableModel dataModel = new AbstractTableModel() {
                 public int getColumnCount() { return 10; }
                 public int getRowCount() { return 10;}
                 public Object getValueAt(int row, int col) { return new Integer(row*col); }
      };
/*
      String[] columnNames = {"", "Left team", "Right Team"};
      DBAnalyzer dbanalyzer = new DBAnalyzer();

      Object[][] data = {
          {"Goal", dbanalyzer.goalAnalyze(17), dbanalyzer.goalAnalyze(17)},
          {"Goal Kick", dbanalyzer.goalAnalyze(17), dbanalyzer.goalAnalyze(17)}
      };

      jStatisticTable.setModel(new DefaultTableModel(dbanalyzer.analyze(17), columnNames));
*/
   }

    private void loadGameName() {
        gameItems = new DefaultListModel();
        DatabaseReader dbReader = new DatabaseReader(null);
        gis = dbReader.getGameInfos();
        for (GameItem i : gis)
            gameItems.addElement(i.getDescription());
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(borderLayout1);
        this.setTitle("Statistic");
        jSplitPane1.setPreferredSize(new Dimension(450, 550));
        jSplitPane1.setDividerSize(2);
        jSplitPane1.setLastDividerLocation(200);
        jGameList.addMouseListener(new StatisticFrame_jGameList_mouseAdapter(this));
        jSplitPane1.add(jScrollPane1, JSplitPane.TOP);
        jSplitPane1.add(jScrollPane2, JSplitPane.BOTTOM);
        this.getContentPane().add(jSplitPane1, java.awt.BorderLayout.CENTER);

        jScrollPane2.getViewport().add(jStatisticTable);
        jScrollPane1.getViewport().add(jGameList);

        jSplitPane1.setOrientation(JSplitPane.VERTICAL_SPLIT);
        pack();
        jSplitPane1.setDividerLocation(200);
    }

    private ArrayList <GameItem> gis;
    private DefaultListModel gameItems;
    public void jGameList_mouseClicked(MouseEvent e) {
            int index = jGameList.locationToIndex(e.getPoint());
            jGameList.ensureIndexIsVisible(index);
            GameItem selectedGame = (GameItem) gis.get(index);
            String[] columnNames = {"", "Left team", "Right Team"};
            DBAnalyzer dbanalyzer = new DBAnalyzer();
            jStatisticTable.setModel(new DefaultTableModel(dbanalyzer.analyze(selectedGame.getId()), columnNames));

  //          JOptionPane.showMessageDialog(this, doc.getDescription(), "Document description", JOptionPane.INFORMATION_MESSAGE);

    }
}


class StatisticFrame_jGameList_mouseAdapter extends MouseAdapter {
    private StatisticFrame adaptee;
    StatisticFrame_jGameList_mouseAdapter(StatisticFrame adaptee) {
        this.adaptee = adaptee;
    }

    public void mouseClicked(MouseEvent e) {
        adaptee.jGameList_mouseClicked(e);
    }
}
